﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using EasyWeixin.CommonAPIs;
using EasyWeixin.Entities.JsonResult;

using RaceList.activity.help;
using RaceList.activity.util;

namespace RaceList.activity.relay
{
    /// <summary>
    ///     ?cycle=123&sponsor=123
    /// </summary>
    public partial class Relayindex : Page, IAuthWeixin
    {
        public List<Paihang> Paihangs { get; set; }
        protected WxConfigResult WxConfig { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            string cycleId = Request.QueryString[Parameters.cycle];

            #region

            if (string.IsNullOrEmpty(cycleId))
            {
                Response.Redirect404(Request.Url.ToString());
            }
            else
            {
                int id = Convert.ToInt32(cycleId);
                Activity_Cycle cycle = RelayQuery.GetCycle(id);
                if (cycle != null)
                {
                    SessionHelp.CycleId = id;
                }
                else
                {
                    Response.Redirect404(Request.Url.ToString());
                }
            }

            #endregion

            #region 赞助商的参数sponsor

            string sponsorId = Request.QueryString[Parameters.sponsor];
            if (!string.IsNullOrEmpty(sponsorId))
            {
                try
                {
                    int id = Convert.ToInt32(sponsorId);
                    if (RelayQuery.GetActivity_Sponsor(id) != null) //只有有效的赞助商才加入到session中
                        SessionHelp.SponsorId = id;
                }
                catch
                {
                }
            }

            #endregion

            InitPaiHangs();
            InitWxConfig();
        }

        public void InitPaiHangs()
        {
            Paihangs = new List<Paihang>();
            List<Activity_Relay> list = RelayQuery.GetPaihang();
            foreach (Activity_Relay item in list)
            {
                Paihangs.Add(new Paihang
                {
                    Headimgurl = item.WeixinUser.Headimgurl,
                    Nickname = item.WeixinUser.Nickname,
                    SumScore = item.SumScore,
                    RelayId = item.Id,
                });
            }
        }

        public void InitWxConfig()
        {
            WxConfigResult result = CommonApi.GetWxConfigResult(ActivityConfig.MHalloweenAppId,
                ActivityConfig.MHalloweenAppSecrect, Request.Url.ToString());
            WxConfig = result;
        }
    }
}